<template>
	<el-card>
		<div v-if="key == this.routePath.defaultPath">
			<!-- 搜索 -->
			<el-form :inline="true" :model="searchForm">
				<el-form-item label="昵称">
					<el-input placeholder="用户昵称" v-model="searchForm.userNickName" clearable></el-input>
				</el-form-item>
				<el-form-item label="手机号">
					<el-input placeholder="手机号" v-model="searchForm.userPhone" clearable></el-input>
				</el-form-item>
				<el-form-item label="性别">
					<el-select v-model="searchForm.userSex" class="input-width" placeholder="全部" clearable>
						<el-option v-for="item in userSexOptions"
							:key="item.value"
							:label="item.label"
							:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="状态">
					<el-select v-model="searchForm.userStatus" class="input-width" placeholder="全部" clearable>
						<el-option v-for="item in userStatusOptions"
							:key="item.value"
							:label="item.label"
							:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
				</el-form-item>
			</el-form>
			<!-- 列表 -->
			<el-table :data="dataList" height="calc(100VH - 270px)" highlight-current-row border style="width: 100%" v-loading="loading">
				<el-table-column label="序号" type="index" align="center" width="50"></el-table-column>
				<el-table-column label="昵称" prop="userNickName" align="center"></el-table-column>
				<el-table-column label="手机号" prop="userPhone" align="center"></el-table-column>
				<el-table-column label="性别" prop="userSex" align="center">
					<template slot-scope="scope">
						<span v-if="scope.row.userSex == 1">男</span>
						<span v-if="scope.row.userSex == 2">女</span>
						<span v-if="scope.row.userSex == 3">保密</span>
					</template>
				</el-table-column>
				<el-table-column label="头像" prop="userAvatar" align="center">
					<template slot-scope="scope">
						<el-avatar size="medium" :src="scope.row.userAvatar" @error="errorHandler">
							<img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png"/>
						</el-avatar>
					</template>
				</el-table-column>
				<el-table-column label="状态" prop="userStatus" align="center" >
					<template slot-scope="scope">
						{{scope.row.userStatus ==1 ? '正常':'禁用'}}
					</template>
				</el-table-column>
				<el-table-column label="最后登陆地址" prop="userLastLoginIp" align="center" width="180"></el-table-column>
				<el-table-column label="最后登陆时间" prop="userLastLoginTime" align="center" width="180"></el-table-column>
				<el-table-column label="操作" align="center" width="150" fixed="right">
					<template slot-scope="scope">
						<el-button v-if="permission_view" type="text" size="small" @click="handleView(scope.row.userCode)">查看</el-button>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页 -->
			<div v-show="!loading" class="pagination-container">
				<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageForm.pageNum"
					:page-sizes="pageForm.pageSizeSelect" :page-size="pageForm.pageSize" :total="pageForm.totalData" layout="total, sizes, prev, pager, next, jumper"
					:prev-text="pageForm.prevText" :next-text="pageForm.nextText">
				</el-pagination>
			</div>
		</div>
		<div v-else>
			<router-view :key="key"></router-view>
		</div>
	</el-card>
</template>

<script>
	import { mapGetters } from 'vuex'
	import { findMemberList } from '@/api/crm/member'
	export default {
		name: 'member',
		data() {
			return {
				// 默认加载弹出层
				loading: true,
				//路由地址
				routePath: {
					defaultPath: '/crm/member',
					viewPath: '/crm/member/view'
				},
				// 数据列表el-table
				dataList: [],
				// 权限按钮控制
				permission_view: false,
				// 搜索条件
				searchForm: {
					userNickName: '',
					userPhone: '',
					userSex: '',
					userStatus: '',
					pageNum: 1,
					pageSize: 10
				},
				//分页相关
				pageForm: {
					pageSizeSelect: [10, 20, 30, 50], //每页显示个数选择器的选项设置
					totalData: 0, //总条数
					pageSize: 10, //每页显示条目个数
					pageNum: 1, //当前页
					prevText: '上一页', //替代图标显示的上一页文字
					nextText: '下一页' //替代图标显示的下一页文字
				},
				userStatusOptions: [
					{
						label: '启用',
						value: 1
					},
					{
						label: '禁用',
						value: 2
					},
				],
				userSexOptions: [
					{
						label: '男',
						value: 1
					},
					{
						label: '女',
						value: 2
					},
					{
						label: '保密',
						value: 3
					},
					
				],
				
			}
		},
		computed: {
			key() {
				return this.$route.path
			},
			...mapGetters([
				'elementList'
			])
		},
		components: {},
		created() {
			if (this.$route.path === this.routePath.defaultPath) {
				// 初始化获取数据列表
				this.getDateList()
				//初始化按钮权限
				this.permission_view = this.elementList['crm:member:detail']
			}
		},
		methods: {
			// 方法集合
			getDateList() {
				this.loading = true
				findMemberList(this.searchForm).then(res => {
					this.dataList = res.data.responseData
					this.pageForm.totalData = parseInt(res.data.total)
				})
				this.loading = false
			},
			handleSearch() {
				this.getDateList();
			},
			handleSizeChange(val) {
				this.searchForm.pageSize = val
				this.getDateList()
			},
			handleCurrentChange(val) {
				this.searchForm.pageNum = val
				this.getDateList()
			},
			errorHandler(){
				return true
			},
			handleView(val){
				this.$router.push({
					path: this.routePath.viewPath,
					query: {
						memberCode: val
					}
				});
			}
			

		}
	}
</script>
